<?php
ini_set('display_errors',1);
set_time_limit(0); 
include_once dirname(dirname(dirname(__FILE__))).'/mgtv_init.php';
include_once dirname(dirname(__FILE__)).'/init.php';
//从任务表取内容，调用C2执行
i_echo('start');
$db_r = nl_get_db(NL_DB_READ);
$db_r->open();

$db_w = nl_get_db(NL_DB_WRITE);
$db_w->open();

$sql="select * from nns_mgtvbk_category_content where  nns_video_type=0 and  nns_status=0 group by nns_org_id,nns_video_id order by nns_create_time asc ";
$all_content = nl_db_get_all($sql, $db_r);

while(list($key, $value) = each($all_content)){
	$nns_org_id = $value['nns_org_id'];
	$nns_vod_id = $value['nns_video_id'];
	//判断是影片还是连续剧
	$sql="select nns_all_index from nns_vod where nns_id='".$nns_vod_id."'";
	$all_index_num = nl_db_get_col($sql, $db_r);unset($sql);
	if($all_index_num==1){//电影
		$sql = "select count(*) from nns_mgtvbk_c2_task where nns_status=0 and nns_type='index' and nns_org_id='".$nns_org_id."' and nns_src_id='".$nns_vod_id."'";
		$num = nl_db_get_col($sql, $db_r);unset($sql);
		if($num==1){
			$sql = "update nns_mgtvbk_category_content set nns_status=1 where nns_video_type=0 and nns_video_id='".$nns_vod_id."' and nns_org_id='".$nns_org_id."'";
			nl_execute_by_db($sql,$db_w);unset($sql);
			i_echo($nns_vod_id.'finish');
		}
	}elseif($all_index_num>=1){//连续剧
		$sql = "select count(*) from nns_mgtvbk_c2_task where nns_type='index' and nns_org_id='".$nns_org_id."' and nns_src_id='".$nns_vod_id."'";
		$index_num = nl_db_get_col($sql, $db_r);unset($sql);
		$sql = "select count(*) from nns_mgtvbk_c2_task where  nns_type='vod' and nns_org_id='".$nns_org_id."' and nns_ref_id='".$nns_vod_id."'";
		$vod_num = nl_db_get_col($sql, $db_r);unset($sql);
		$all_num = $index_num+$vod_num;
		
		$sql = "select count(*) from nns_mgtvbk_c2_task where nns_status=0 and nns_type='index' and nns_org_id='".$nns_org_id."' and nns_src_id='".$nns_vod_id."'";
		$index_fisinh_num = nl_db_get_col($sql, $db_r);unset($sql);
		$sql = "select count(*) from nns_mgtvbk_c2_task where nns_status=0 and nns_type='vod' and nns_org_id='".$nns_org_id."' and nns_ref_id='".$nns_vod_id."'";
		$vod_fisinh_num = nl_db_get_col($sql, $db_r);unset($sql);
		$all_fisinh_num = $index_fisinh_num+$vod_fisinh_num;
		if($all_num==$all_fisinh_num){
			$sql = "update nns_mgtvbk_category_content set nns_status=1 where nns_video_type=0 and nns_video_id='".$nns_vod_id."' and nns_org_id='".$nns_org_id."'";
			nl_execute_by_db($sql,$db_w);unset($sql);
			i_echo($nns_vod_id.'finish');
		}		
	}
}

i_echo('end');
